//导入express框架
var express = require("express");
var app = express();

//解决跨域问题
const cors = require("cors");

// 中间件 获取参数的
const bodyParser = require("body-parser");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());

app.all("*", function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  // res.header("Access-Control-Allow-Headers", "X-Requested-With");
  // res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OP0TIONS");
  // res.header("X-Powered-By", "3.2.1");
  next();
});

// 自定义中间件
const mw = require("./middleware/mw");
app.use(mw);

// 托管静态资源
app.use("/public", express.static("public"));
app.use("/nav-images", express.static("D:\\图库\\导航截图\\"));
app.use(
  "/video",
  express.static("C:\\Users\\bleege-1014\\Desktop\\测试资源\\测试视频")
);

// base路径
const BASE_URL = "/api";

// 路由模块
const testApi = require("./router/testApi");
app.use(BASE_URL + "/test", testApi);

const loginApi = require("./router/login");
app.use(BASE_URL + "/login", loginApi);

// 获取网页信息模块
const webInfoApi = require("./router/webInfo");
app.use(BASE_URL + "/webInfo", webInfoApi);

// 获取网页链接
const crawlLinkApi = require("./router/crawlLink");
app.use(BASE_URL + "/crawlLink", crawlLinkApi);

// mqtt服务
const mqttApi = require("./router/testMqtt");
app.use(BASE_URL + "/mqtt", mqttApi);

var server = app.listen(3000, function () {
  var port = server.address().port;

  console.log("服务启动成功", `http://127.0.0.1:${port}`);
});
